import db from "@/server/connector/db";
import { PostSummary } from "@/shared/types";
import { User } from "@prisma/client";

async function pageGetPostSummary(
  me: User,
  page: number,
  size: number,
): Promise<{ posts: PostSummary[]; count: number }> {
  const [posts, count] = await Promise.all([
    db.post.findMany({
      select: { id: true, title: true, createTime: true, updateTime: true, contentType: true, personal: true },
      where: { authorId: me.id, deleteTime: null },
      orderBy: { createTime: "desc" },
      skip: (page - 1) * size,
      take: size,
    }),
    db.post.count({ where: { authorId: me.id, deleteTime: null } }),
  ]);
  return { posts, count };
}

const myUndelPostsService = {
  pageGetPostSummary,
};

export default myUndelPostsService;
